import { AuthService } from '@service/auth.service';
import { ResetPasswordComponent } from '@share/component/reset-password/reset-password.component';
import { NzModalService } from 'ng-zorro-antd/modal';
import { NzMessageService } from 'ng-zorro-antd/message';
import { LoginService } from '@service/login.service';
import { Component, OnInit } from '@angular/core';
import { NavigationEnd, Router } from '@angular/router';
import { filter } from 'rxjs/operators';
import { environment } from '@env/environment';
import { NzNotificationService } from 'ng-zorro-antd/notification';
import { VERSION_VALUE } from '@common/static';
import { MenuService } from '@service/menu.service';
import { WebsocketService } from '@service/websocket.service';
import { API_ROOT } from '@core/core.contants';
@Component({
	selector: 'app-page',
	templateUrl: './page.component.html',
	styleUrls: ['./page.component.less']
})
export class PageComponent implements OnInit {
	isCollapsed = true;
	userInfo = null;

	version = VERSION_VALUE;
	isLoading = false;
	noticeNum = 0;
	isMsg = false;

	menuList = [];

	breadcrumbList= [];
	noticeList =[];
	constructor(
		private loginService: LoginService,
		private nzMessageService: NzMessageService,
		private router: Router,
		private nzModalService: NzModalService,
		private authService: AuthService,
		private menuService: MenuService,
		private websocketService: WebsocketService,
		private nzNotificationService: NzNotificationService

	) {
		this.menuList = this.menuService.getMenu();
	}
	ngOnInit() {
		this.init();
		this.router.events
		.pipe(
			filter((event) => event instanceof NavigationEnd)
		)
		.subscribe((event:NavigationEnd) => {
			const path = event.url;
			this.getBreadcrumb(path);
		});
	}

	initSocket() {
		// const url = API_ROOT.default + '/websocket?userCode=' + this.userInfo?.usercode;
		// if (this.websocketService.ws) {
		// 	return;
		// }
		// this.websocketService.createObservableSocket(url.replace(/http/g, 'ws').replace(/https/g, 'wss')).subscribe(
		// 	data => {
		// 		const result = JSON.parse(data)
		// 		this.noticeNum = this.noticeNum + 1;
		// 		this.isMsg = true;
		// 		this.nzNotificationService.success("", result.content.message);
		// 	},
		// 	err => console.error(err),
		// 	() => console.warn("流已经结束") //  最后结束后，会执行到这的
		// );

	}
	getBreadcrumb(path: string) {
		const menuList = this.menuList;
		const breadcrumbList = [];
		menuList.forEach(menu => {
			if (menu.router.includes(path)) {
				breadcrumbList.push(menu);
			}
			if (menu?.children?.length) {
				const res = menu?.children.find(it => it?.router === path);
				if (res) {
					breadcrumbList.push(menu, res);
				}
			}
		});
		this.breadcrumbList = breadcrumbList;
	}

	async init() {
		this.userInfo = this.loginService.getUserInfo();
		this.getBreadcrumb(this.router.url);
		// this.initSocket();
	}

	async getNoticeList(val: boolean) {
		if (!val) {
			return;
		}
		// this.websocketService.sendMessage('233');
		this.isMsg = false;
		this.isLoading = true;
		// const  result = await this.baseService.getNoticeList({page: 1, limit:30, status: '0' });
		// this.noticeList = result.data.list;
		setTimeout(() => {
			this.isLoading = false;
		}, 500);
	}


	async logout() {
		await this.loginService.logout();
		this.nzMessageService.success('退出成功');
		this.loginService.clearUserInfo();
		this.router.navigateByUrl('/login');
	}


	resetPassword() {
		const _this = this;
		this.nzModalService.create({
			nzTitle: '更改密码',
			nzContent: ResetPasswordComponent,
			nzWidth: 600,
			nzFooter: null,
			nzOnOk: () => {
				_this.logout();
			}
		})
	}
}
